<%@ page language="java" pageEncoding="UTF-8" %>
<%@include file="/WEB-INF/view/common/common.jsp" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/resources/mallcss/initcss.css" rel="stylesheet">
    <link href="${ctx}/resources/mallcss/common.css" rel="stylesheet">
    <%--<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">--%>
    <link href="${ctx}/resources/mallcss/sliderBar.css" rel="stylesheet">
    <style>

        .con {
            background-color: #ebebeb;
            position: relative;
        }

        header {
            background: #f03791;
            height: 3.7em;
            text-align: center;
            position: relative;
        }

        .Return, .Home {
            position: absolute;
            width: 3.7em;
            height: 3.7em;
        }

        .Return {
            left: 0;
        }

        .Home {
            right: 0;
        }

        .Return span, .Home span {
            background: url("${ctx}/resources/mallimages/png.png");
            background-size: 17em;
            position: absolute;
            width: 1.7em;
            height: 1.7em;
        }

        .Return span {
            background-position: -0.17em -5.5em;
            left: 1em;
            top: 1em;
        }

        .Home span {
            background-position: -0.17em -8.1em;
            left: 1em;
            top: 1em;
        }

        .Title {
            color: white;
            font-size: 1.3em;
            line-height: 2.7em;
        }

        .bar {
            overflow: hidden;
            border-bottom: 1px solid #ebebeb;
            background-color: #FEFFFE;
            padding: 1em 1.5em;
            font-size: 1.2em;
        }

        .bar .info {
            float: left;
        }

        .bar .info i {
            margin-left: 1em;
            font-size: 1em;
            color: #a5a5a5;
        }

        .bar .barBtn {
            float: right;
        }

        .bar .barBtn .reSet {
            padding-right: 1em;
        }

        .bar .barBtn .edit {
            padding-left: 1em;
        }

        .bar .barBtn i {
            color: #a5a5a5;
        }

        .accounts ul li {
            background-color: #FEFFFE;
            border-bottom: 1px solid #ebebeb;
            padding: 0.5em 0.5em;
            overflow: hidden;
            position: relative;

        }

        .accounts ul li img {
            width: 4em;
            border-radius: 4em;
            float: left;
        }

        .accounts .accountsInfo {
            margin-left: 1em;
            display: inline-block;
            padding-top: 0.6em;
        }

        .accounts .accountsInfo h4 {
            color: #a5a5a5;
            padding-top: 0.8em;

        }

        .accounts .accountsInfo h3.hidden {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 5em;

        }

        .operate {
            float: right;

        }

        .operate .inputs {

            /*float: left;*/
            display: inline-block;
            margin-right: 5em;
            line-height: 4em;

        }

        .inputs span {
            display: inline-block;
            width: 28px;
            height: 28px;
            line-height: 28px;
            text-align: center;
            font-size: 18px;
            color: #999;
            cursor: pointer;
            border: 1px solid #e6e6e6;
        }

        .inputs input {
            border: none;
            outline: none;
            font-size: 1em;
            width: 3em;
            text-align: center;
        }

        .delete {
            position: absolute;
            top: 0;
            right: 0;
            /*display: inline-block;*/
            background-color: red;
            text-align: center;
            color: white;
            width: 3.5em;
            height: 100%;
            line-height: 5em;

        }

        .btns {
            background-color: #fefffe;
            overflow: hidden;
        }

        .transfer, .backTransfer {

            text-align: center;
            width: 33%;
            text-align: center;
            display: inline-block;
            font-size: 1.5em;
            padding: 0.5em 0;
            color: black;
            cursor: pointer;
            /*-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .1);*/
        }

        .backTransfer {
            width: 100%;
        }

        .freeTransfer {
            width: 33%;
            text-align: center;
            color: #26ac26;
            float: right;
            display: inline-block;
            font-size: 1.5em;
            padding: 0.5em 0;
            cursor: pointer;
            border-left: 1px solid #e6e6e6;
            margin-left: -1px;
        }

        .delivery, .mydelivery {
            width: 50%;
            text-align: center;
            color: #26ac26;
            float: right;
            display: inline-block;
            font-size: 1.5em;
            padding: 0.5em 0;
            cursor: pointer;
            border-left: 1px solid #e6e6e6;
            margin-left: -1px;
            /*-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .1);*/
        }

        .mydelivery {
            width: 100%;
        }

        .warn {
            padding: 1em 1em;
            color: #a5a5a5;
            font-size: 1.1em;
        }

        .warn ul li {
            padding: 0.25em 0;
        }

        .item i {
            float: right;
            color: red;
        }

        /**地质层**/
        .address {
            /*margin-top: 1em;*/
            padding: 0.5em 1em 0.5em 0.5em;
            background-color: #ffffff;
            height: auto;
            position: relative;
            border-bottom: dashed 1px #dedede;
        }

        .address:first-child {
            border-top: dashed 1px #dedede;
        }

        .address .info {
            position: relative;
            overflow: hidden;
        }

        .info .addrImg {
            width: 3.7em;
            height: 3.7em;
            float: left;
            position: relative;
        }

        .addrImg span {
            width: 1.7em;
            height: 1.7em;
            background: url("${ctx}/resources/mallimages/png.png") -15.65em 0 no-repeat;
            background-size: 17em;
            font-size: 1em;
            line-height: 2em;
            position: absolute;
            top: 1.2em;
            left: 1em;
        }

        .info p {
            margin-top: 0.5em;
        }

        .info .adrPhone {
            position: absolute;
            right: 1em;
        }

        .info .adrDetail {
            margin-top: 1em;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            /*position: absolute;*/
            /*bottom: 0;*/
        }

        .address a {
            position: absolute;
            top: 1em;
            right: 0;
            height: 3.7em;
            width: 3.7em;
        }

        .address a span {
            display: none;
            width: 1.7em;
            height: 1.7em;
            background: url("${ctx}/resources/mallimages/png.png") -6.5em -5.2em no-repeat;
            background-size: 17em;
            /*position: absolute;*/
            float: right;
            margin-right: 0.5em;
            margin-top: 0.68em;
            /*top: 0.5em;*/
            /*left: 1em;*/
        }

        nav {
            overflow: scroll;

        }

        #selectAddress {
            margin-bottom: 5em;
        }


    </style>
    <title>进货</title>
</head>
<body style="background: rgb(232, 232, 232);">
<div id="st-container" class="st-container" style="overflow: scroll;">
    <%--<header>--%>
    <%--<a href="" class="Return"><span></span></a>--%>
    <%--<span class="Title">选择产品</span>--%>
    <%--<a href="" class="Home"><span></span></a>--%>
    <%--</header>--%>

   <%-- <div class="bar">
        <div class="info">
            &lt;%&ndash;<span>给${toAgent.realName}转货</span>&ndash;%&gt;
            &lt;%&ndash;<i class="fa fa-angle-right" aria-hidden="true"></i>&ndash;%&gt;
        </div>
        <div class="barBtn">
            <span class="reSet">重置</span>
            <i>|</i>
            <span class="edit">编辑</span>
        </div>
    </div>--%>

    <div class="accounts">
        <ul>
            <li style="text-align: center;" >
                <div>
                    <h1 style="font-weight: bold;font-size: 16px">进货人：<span>${agent.realName}</span></h1>
                </div>
            </li>
            <li style="text-align: center;">
                <div>
                    <h1 style="font-weight: bold;font-size: 16px">编号：<span>${agent.agentCode}</span></h1>
                </div>
            </li>
            <c:forEach items="${list}" var="good">
                <%--<c:if test="${accounts.currentBalance!=0}">--%>
                    <li class="item" data-id="${good.id}">
                        <img src="${dmzImgPath}${good.actImg}" alt="">
                        <div class="accountsInfo">
                            <h3>${good.name}</h3>
                            <h4>数量:${good.num}</h4>

                        </div>
                    </li>

                <%--</c:if>--%>
            </c:forEach>
            <li style="text-align: center">
                <div>
                    <h2>总金额：<input id="amount1" style="width: 70px" disabled="disabled" type="text" value="${zong}">元</h2>
                </div>
            </li>

        </ul>
    </div>

        <div class="warn">
            <ul>
                <li>温馨提示 :</li>
                <br>
                <li style="/*color: #ab0000;*/font-size: 10px;">
                    1.点击个人中心中的"发货转货",选择收货人
                </li>
                <br>
                <li style="font-size: 10px;">
                    2.发货:点击"我要发货"  转货:点击下级账号进入账号面板,再点击"给他转货"
                </li>
                <br>
                <%--<li>--%>
                <%--*赠送，将虚拟库存转入客户系统，不会计算利润--%>
                <%--</li>--%>
                <li style="font-size: 10px;">
                    3.选中要发货/转货的商品,点击右上角的编辑,输入要发货/转货的数量
                </li>
                <br>
                <li style="font-size: 10px;">
                    4.点击我要发货,可以选择已保存的地址,也可以新增地址,确认发货
                </li>
                <br>
                <li style="font-size: 10px;">
                    5.点击我要转货,填写备注,确认转货
                </li>
            </ul>
        </div>
</div>


</div>
<jsp:include page="/WEB-INF/view/common/footer.jsp"/>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script src="${ctx}/resources/malljs/classie.js"></script>
<script src="${ctx}/resources/malljs/sidebarEffects.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/distpicker/2.0.0-rc/distpicker.min.js"></script>
<script>
    $(function () {

        var isEdit = false;

        var canEdit = false;

        //显示所有点击了的
        function showOperate() {

//        $("").each(function (v,i) {
//            $(v).find()
//        })
            $(".item").hide();
            $(".item.click").show();

            $(".item.click i").hide();
            $(".item.click .operate").show();
            $(".item.click .accountsInfo h3").addClass("hidden");
            $(".btns").show();

        }

//    function hideOperate() {
//        $(".operate").hide();
//        $(".accountsInfo h3").removeClass("hidden");
//        $(".btns").hide();
//    }

        var zong=0;
        //增加
        $(".add").click(function () {
            var inp = $(this).closest("div").children(".amount");
            /*var shu=parseInt(inp.val()) + 1;*/
            var jg = $(this).closest("div").children(".num").val();
            zong=parseInt(parseInt(zong)+parseInt(jg));
            $("#amount1").val(parseInt(zong));
            inp.val(parseInt(inp.val()) + 1);
        });

        //增加
        $(".reduce").click(function () {
            var inp = $(this).closest("div").children(".amount");
            if (parseInt(inp.val() - 1) < 0) {
                $(this).val(0);
                return;
            }
           /* var shu=parseInt(inp.val()) - 1;*/
            var jg = $(this).closest("div").children(".num").val();
            zong=parseInt(parseInt(zong)-parseInt(jg));
            $("#amount1").val(parseInt(zong));
            inp.val(parseInt(inp.val()) - 1);
        });


        //点击
        $(".item").click(function () {
//            $("#addBtn").button("loading");
            canEdit = true;
            if (isEdit)return;
            var gid = $(".item").attr("data-id");
            $(this).addClass("click");
            $(this).find("i").show();
            //重置亮起
            $(".edit").css("color", "red");
        });


        //编辑
        $(".edit").click(function () {
            if (!canEdit)return;
            isEdit = true;
            showOperate();
            $(this).css("color", "black");
            $(".reSet").css("color", "red");
        });

        //重置
        $(".reSet").click(function () {
            window.location.reload();
        });

        //删除
        $(".delete").click(function () {
            $(this).closest("li").hide();
            $(this).closest("li").removeClass("click");//移除click
            $(this).closest("li").find("i").hide();
            var sun=$(this).closest("div").parents().children(".inputs").children(".amount").val();
            var num=$(this).closest("div").parents().children(".inputs").children(".num").val();
            zong=parseInt(zong)-parseInt(num)*parseInt(sun);
            $("#amount1").val(zong);
        });

        //限制只能输入整数
        $(".amount").keyup(function () {
            var tmptxt = $(this).val();
            $(this).val(tmptxt.replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            var tmptxt = $(this).val();
            $(this).val(tmptxt.replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");

        //地址编辑

        //新增地址
        /*$("#addBtn").click(function () {
            $("#addAddress").show();
            $("#selectAddress").hide();
        });*/

        //产品Id
        var goodsIds = [];

        //产品数量
        var amounts = [];

        function checkGoods() {
            //清空
            goodsIds = [];
            amounts = [];
            /*$(".item.click").each(function (i, v) {
                var id = $(v).attr("data-id");
                var amount = $(v).find(".amount").val();
                goodsIds.push(id);
                amounts.push(amount);
            });*/
            <c:forEach items="${list}" var="good">
                var id=${good.id};
                var amount=${good.num};
                goodsIds.push(id);
                amounts.push(amount);
            </c:forEach>
        }

        $(".freeTransfer").click(function () {
            if(!confirm("确定赠送吗?")){
                return;
            }
            checkGoods();
            var isFree = true;
            var remark = prompt("如果需要备注，请填写!");
            if (remark == "") remark = "未备注";
            var params = {
                "remark": remark,
                "goodsIds": goodsIds,
                "amounts": amounts,
                "toUid":${agent.id},
                "isFree":isFree
            };
            applyTransfer(params);
        });


        //确定转货
        $(".transfer").click(function () {
            checkGoods();
            var isFree = false;
            var remark = prompt("如果需要备注，请填写!");
            if (remark == "") remark = "未备注";
            var params = {
                "remark": remark,
                "goodsIds": goodsIds,
                "amounts": amounts,
                "uid":${user.id},
                "toUid":${agent.id},
                "isFree":isFree
            };
            applyTransfer(params);
        });

        //拨货
        function applyTransfer(params) {
            params = $.param(params, true);
            $.post("<c:url value='/mobile/goods/transfer2.json'/>",
                params,
                function (data, status, jqXHR) {
                    var m = data;
                    if (m.flag == "0") {
                        alert("转货成功!");
                        window.location.href = "<c:url value='/mobile/transfer/records.html'/>";
                    } else {
                        alert("操作失败,请重试" + m.message);
//                        window.location.reload();
                    }
                }).fail(function (xhr) {
                if (xhr.status == 401) {
                    window.location.href = xhr.getResponseHeader("Location");
                } else {
                    console.log(xhr);
                    alert("未知错误请联系管理员" + xhr.status);
//                    window.location.reload();
                }
            });
        }

        //申请退货
        //确定转货
        $(".backTransfer").click(function () {
            checkGoods();
            var remark = prompt("如果需要备注，请填写");
            if (remark == "") remark = "未备注";
            var params = {
                "remark": remark,
                "goodsIds": goodsIds,
                "amounts": amounts
            };
            applyBackTransfer(params);
        });

        //退货
        function applyBackTransfer(params) {
            params = $.param(params, true);
            $.post("<c:url value='/mobile/goods/backTransfer.json'/>",
                params,
                function (data, status, jqXHR) {
                    var m = data;
                    if (m.flag == "0") {
                        alert("申请退货成功，等待管理员处理!");
                        window.location.href = "<c:url value='/mobile/transfer/records.html'/>";
                    } else {
                        alert("操作失败" + m.message);
//                        window.location.reload();
                    }
                }).fail(function (xhr) {
                if (xhr.status == 401) {
                    window.location.href = xhr.getResponseHeader("Location");
                } else {
                    console.log(xhr);
                    alert("未知错误请联系管理员" + xhr.status);
//                    window.location.reload();
                }
            });
        }


        //点击选择了地址
        $(".address").click(function () {
            checkGoods();//核算点击的产品
            var aid = $(this).children(".aid").val();
            if (confirm("确定发给这个地址？")) {
                $("#addBtn").button("loading");
                var remark = prompt("如果需要备注，请填写");
                if (remark == "") remark = "未备注";
                var params = {
                    "id": aid,
                    "remark": remark,
                    "goodsIds": goodsIds,
                    "amounts": amounts,
                    "toUid":${agent.id}
                };
                applyDelivery(params);
            }
        });


        //确定发货
        $("#submitBtn").click(function () {
            $(this).button("loading");
            checkGoods();//核算点击的产品
//        console.log(goodsIds);
//        console.log(amounts);

//            var remark = "未备注";
            var remark = prompt("如果需要备注，请填写");
            if (remark == "") remark = "未备注";
            var addrName = $("#addrName").val();
            if (addrName == "") {
                alert("名字不能为空");
                return;
            }
            var addrCode = $("#addrCode").val();
            var addrPhone = $("#addrPhone").val();
            if (addrPhone == "") {
                alert("电话不能为空");
                return;
            }
            var addrProvince = $("#addrProvince").val();
            if (addrProvince == "") {
                alert("省份不能为空");
                return;
            }
            var addrCity = $("#addrCity").val();
            if (addrCity == "") {
                alert("市区不能为空");
                return;
            }
            var addrCounty = $("#addrCounty").val();
//            if (addrCounty == "") {
//                alert("县不能为空");
//                return;
//            }
            var address = $("#address").val();
            if (address == "") {
                alert("详细地址不能为空");
                return;
            }
            var params = {
                "remark": remark,
                "consignee": addrName,
                "consigneeCode": addrCode,
                "mobile": addrPhone,
                "province": addrProvince,
                "city": addrCity,
                "county": addrCounty,
                "country": "中国",
                "address": address,
                "goodsIds": goodsIds,
                "amounts": amounts,
                "toUid":${agent.id}
            };
            applyDelivery(params);
        });

        //直接发货
        function applyDelivery(params) {
            params = $.param(params, true);
            $.post("<c:url value='/mobile/goods/delivery.json'/>",
                params,
                function (data, status, jqXHR) {
                    var m = data;
                    if (m.flag == "0") {
                        alert("发货成功!");
                        window.location.href = "<c:url value='/mobile/delivery/records.html'/>";
                    } else {
                        alert("操作失败" + m.message);
//                        window.location.reload();
                    }
                }).fail(function (xhr) {
                if (xhr.status == 401) {
                    window.location.href = xhr.getResponseHeader("Location");
                } else {
                    console.log(xhr);
                    alert("未知错误请联系管理员" + xhr.status);
//                    window.location.reload();
                }
            });
        }


    })


</script>

</body>
</html>